package com.yq.topCode;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName Solution141hascycle
 * @Description 已提交，用龟兔赛跑和hashset
 * @Author yaoqiang
 * @Date 2022/8/12 9:26
 * @Version 1.0
 */
public class Solution141hascycle {
    public static void main(String[] args) {

    }
    public boolean hasCycle(ListNode head){
        if(head==null|| head.next==null)return false;
        ListNode slow=head;
        ListNode fast=head.next;
        while (slow!=fast){
            if(fast==null|| fast.next==null)return false ;
            slow=slow.next;
            fast=fast.next.next;
        }
        return true;
    }
    public boolean hasCycle2(ListNode head){
        Set<ListNode> visited=new HashSet<>();
        while (head!=null){
            if(visited.contains(head)){
                return true;
            }
            visited.add(head);
            head=head.next;
        }
        return  false;
    }
}
